/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.    
*/

namespace NVelocity.Runtime.Parser
{
    using System;
    using System.IO;
    using System.Text;

    /// <summary>  NOTE : This class was originally an ASCII_CharStream autogenerated
    /// by Javacc.  It was then modified via changing class name with appropriate
    /// fixes for CTORS, and mods to readChar().
    /// 
    /// This is safe because we *always* use Reader with this class, and never a
    /// InputStream.  This guarantees that we have a correct stream of 16-bit
    /// chars - all encoding transformations have been done elsewhere, so we
    /// believe that there is no risk in doing this.  Time will tell :)
    /// </summary>

    /// <summary> An implementation of interface CharStream, where the stream is assumed to
    /// contain only ASCII characters (without unicode processing).
    /// </summary>

    public sealed class VelocityCharStream : ICharStream
    {
        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.getEndColumn()">
        /// </seealso>
        public int EndColumn
        {
            get
            {
                return bufcolumn[bufpos];
            }

        }
        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.getEndLine()">
        /// </seealso>
        public int EndLine
        {
            get
            {
                return bufline[bufpos];
            }

        }
        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.getBeginColumn()">
        /// </seealso>
        public int BeginColumn
        {
            get
            {
                return bufcolumn[tokenBegin];
            }

        }
        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.getBeginLine()">
        /// </seealso>
        public int BeginLine
        {
            get
            {
                return bufline[tokenBegin];
            }

        }
        public const bool staticFlag = false;
        internal int bufsize;
        private int nextBufExpand;
        internal int available;
        internal int tokenBegin;

        public int bufpos = -1;
        private int[] bufline;
        private int[] bufcolumn;

        private int column = 0;
        private int line = 1;

        private bool prevCharIsCR = false;
        private bool prevCharIsLF = false;

        private TextReader inputStream;

        private char[] buffer;
        private int maxNextCharInd = 0;
        private int inBuf = 0;

        private void ExpandBuff(bool wrapAround)
        {
            char[] newbuffer = new char[bufsize + nextBufExpand];
            int[] newbufline = new int[bufsize + nextBufExpand];
            int[] newbufcolumn = new int[bufsize + nextBufExpand];

            try
            {
                if (wrapAround)
                {
                    Array.Copy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
                    Array.Copy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
                    buffer = newbuffer;

                    Array.Copy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
                    Array.Copy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
                    bufline = newbufline;

                    Array.Copy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
                    Array.Copy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
                    bufcolumn = newbufcolumn;

                    maxNextCharInd = (bufpos += (bufsize - tokenBegin));
                }
                else
                {
                    Array.Copy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
                    buffer = newbuffer;

                    Array.Copy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
                    bufline = newbufline;

                    Array.Copy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
                    bufcolumn = newbufcolumn;

                    maxNextCharInd = (bufpos -= tokenBegin);
                }
            }
            catch (System.Exception t)
            {
                throw new System.ApplicationException(t.Message);
            }


            bufsize += nextBufExpand;
            nextBufExpand = bufsize;
            available = bufsize;
            tokenBegin = 0;
        }

        private bool FillBuff()
        {
            if (maxNextCharInd == available)
            {
                if (available == bufsize)
                {
                    if (tokenBegin > nextBufExpand)
                    {
                        bufpos = maxNextCharInd = 0;
                        available = tokenBegin;
                    }
                    else if (tokenBegin < 0)
                    {
                        bufpos = maxNextCharInd = 0;
                    }
                    else
                    {
                        ExpandBuff(false);
                    }
                }
                else if (available > tokenBegin)
                {
                    available = bufsize;
                }
                else if ((tokenBegin - available) < nextBufExpand)
                {
                    ExpandBuff(true);
                }
                else
                {
                    available = tokenBegin;
                }
            }

            int i;
            try
            {
                if ((i = inputStream.Read(buffer, maxNextCharInd, available - maxNextCharInd)) <= 0)
                {
                    inputStream.Close();
                    throw new System.IO.IOException();
                }
                else
                {
                    maxNextCharInd += i;
                }
                return true;
            }
            catch
            {
                --bufpos;
                Backup(0);
                if (tokenBegin == -1)
                {
                    tokenBegin = bufpos;
                }

                return false;
            }
        }

        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.BeginToken()">
        /// </seealso>
        public char BeginToken()
        {
            tokenBegin = -1;
            char c = ReadChar();
            tokenBegin = bufpos;

            return c;
        }

        private void UpdateLineColumn(char c)
        {
            column++;

            if (prevCharIsLF)
            {
                prevCharIsLF = false;
                line += (column = 1);
            }
            else if (prevCharIsCR)
            {
                prevCharIsCR = false;
                if (c == '\n')
                {
                    prevCharIsLF = true;
                }
                else
                {
                    line += (column = 1);
                }
            }

            switch (c)
            {

                case '\r':
                    prevCharIsCR = true;
                    break;

                case '\n':
                    prevCharIsLF = true;
                    break;

                case '\t':
                    column--;
                    column += (8 - (column & 7));
                    break;

                default:
                    break;

            }

            bufline[bufpos] = line;
            bufcolumn[bufpos] = column;
        }

        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.readChar()">
        /// </seealso>
        public char ReadChar()
        {
            if (inBuf > 0)
            {
                --inBuf;

                /*
                *  was : return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
                */
                return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
            }

            if (++bufpos >= maxNextCharInd)
            {
                if (!FillBuff())
                {
                    throw new IOException();
                }
            }

            /*
            *  was : char c = (char)((char)0xff & buffer[bufpos]);
            */
            char c = buffer[bufpos];

            UpdateLineColumn(c);
            return (c);
        }

        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.backup(int)">
        /// </seealso>
        public void Backup(int amount)
        {

            inBuf += amount;
            if ((bufpos -= amount) < 0)
                bufpos += bufsize;
        }

        /// <param name="dstream">
        /// </param>
        /// <param name="startline">
        /// </param>
        /// <param name="startcolumn">
        /// </param>
        /// <param name="buffersize">
        /// </param>
        public VelocityCharStream(TextReader dstream, int startline, int startcolumn, int buffersize)
        {
            inputStream = dstream;
            line = startline;
            column = startcolumn - 1;

            available = bufsize = nextBufExpand = buffersize;
            buffer = new char[buffersize];
            bufline = new int[buffersize];
            bufcolumn = new int[buffersize];
        }

        /// <param name="dstream">
        /// </param>
        /// <param name="startline">
        /// </param>
        /// <param name="startcolumn">
        /// </param>
        public VelocityCharStream(TextReader dstream, int startline, int startcolumn)
            : this(dstream, startline, startcolumn, 4096)
        {
        }
        /// <param name="dstream">
        /// </param>
        /// <param name="startline">
        /// </param>
        /// <param name="startcolumn">
        /// </param>
        /// <param name="buffersize">
        /// </param>
        public void ReInit(TextReader dstream, int startline, int startcolumn, int buffersize)
        {
            inputStream = dstream;
            line = startline;
            column = startcolumn - 1;

            if (buffer == null || buffersize != buffer.Length)
            {
                available = bufsize = nextBufExpand = buffersize;
                buffer = new char[buffersize];
                bufline = new int[buffersize];
                bufcolumn = new int[buffersize];
            }
            prevCharIsLF = prevCharIsCR = false;
            tokenBegin = inBuf = maxNextCharInd = 0;
            bufpos = -1;
        }

        /// <param name="dstream">
        /// </param>
        /// <param name="startline">
        /// </param>
        /// <param name="startcolumn">
        /// </param>
        public void ReInit(TextReader dstream, int startline, int startcolumn)
        {
            ReInit(dstream, startline, startcolumn, 4096);
        }
        /// <param name="dstream">
        /// </param>
        /// <param name="startline">
        /// </param>
        /// <param name="startcolumn">
        /// </param>
        /// <param name="buffersize">
        /// </param>
        public VelocityCharStream(Stream dstream, int startline, int startcolumn, int buffersize)
            : this(new StreamReader(dstream, Encoding.Default), startline, startcolumn, buffersize)
        {
        }

        /// <param name="dstream">
        /// </param>
        /// <param name="startline">
        /// </param>
        /// <param name="startcolumn">
        /// </param>
        public VelocityCharStream(Stream dstream, int startline, int startcolumn)
            : this(dstream, startline, startcolumn, 4096)
        {
        }

        /// <param name="dstream">
        /// </param>
        /// <param name="startline">
        /// </param>
        /// <param name="startcolumn">
        /// </param>
        /// <param name="buffersize">
        /// </param>
        public void ReInit(Stream dstream, int startline, int startcolumn, int buffersize)
        {
            ReInit(new StreamReader(dstream, Encoding.Default), startline, startcolumn, buffersize);
        }
        /// <param name="dstream">
        /// </param>
        /// <param name="startline">
        /// </param>
        /// <param name="startcolumn">
        /// </param>
        public void ReInit(Stream dstream, int startline, int startcolumn)
        {
            ReInit(dstream, startline, startcolumn, 4096);
        }
        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.GetImage()">
        /// </seealso>
        public string GetImage()
        {
            if (bufpos >= tokenBegin)
            {
                return new string(buffer, tokenBegin, bufpos - tokenBegin + 1);
            }
            else
            {
                return new string(buffer, tokenBegin, bufsize - tokenBegin) + new string(buffer, 0, bufpos + 1);
            }
        }

        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.GetSuffix(int)">
        /// </seealso>
        public char[] GetSuffix(int len)
        {
            char[] ret = new char[len];

            if ((bufpos + 1) >= len)
            {
                Array.Copy(buffer, bufpos - len + 1, ret, 0, len);
            }
            else
            {
                Array.Copy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
                Array.Copy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
            }

            return ret;
        }

        /// <seealso cref="org.apache.velocity.runtime.parser.CharStream.Done()">
        /// </seealso>
        public void Done()
        {
            buffer = null;
            bufline = null;
            bufcolumn = null;
        }

        /// <summary> Method to adjust line and column numbers for the start of a token.<BR></summary>
        /// <param name="newLine">
        /// </param>
        /// <param name="newCol">
        /// </param>
        public void AdjustBeginLineColumn(int newLine, int newCol)
        {
            int start = tokenBegin;
            int len;

            if (bufpos >= tokenBegin)
            {
                len = bufpos - tokenBegin + inBuf + 1;
            }
            else
            {
                len = bufsize - tokenBegin + bufpos + 1 + inBuf;
            }

            int i = 0, j = 0, k = 0;
            int nextColDiff = 0, columnDiff = 0;

            while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
            {
                bufline[j] = newLine;
                nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
                bufcolumn[j] = newCol + columnDiff;
                columnDiff = nextColDiff;
                i++;
            }

            if (i < len)
            {
                bufline[j] = newLine++;
                bufcolumn[j] = newCol + columnDiff;

                while (i++ < len)
                {
                    if (bufline[j = start % bufsize] != bufline[++start % bufsize])
                        bufline[j] = newLine++;
                    else
                        bufline[j] = newLine;
                }
            }

            line = bufline[j];
            column = bufcolumn[j];
        }
    }
}